package cn.fanqinghong.ymcc.web.controller;

import cn.fanqinghong.ymcc.domain.AlipayInfo;
import cn.fanqinghong.ymcc.domain.PayOrder;
import cn.fanqinghong.ymcc.dto.AlipayNotifyDto;
import cn.fanqinghong.ymcc.dto.CoursePayDto;
import cn.fanqinghong.ymcc.dto.NotifyDto;
import cn.fanqinghong.ymcc.result.JSONResult;
import cn.fanqinghong.ymcc.service.IPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * FAN
 * 2022/8/1  14:07
 */
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private IPayOrderService payOrderService;

    /**
     * 检查支付单是否存在
     * @param orderNo
     * @return
     */
    @GetMapping("/checkPayOrder/{orderNo}")
    public JSONResult checkPayOrder(@PathVariable("orderNo") String orderNo){
        PayOrder payOrder = payOrderService.selectByOrderNo(orderNo);
        return payOrder == null ? JSONResult.error():JSONResult.success();
    }

    /**
     * 向支付宝发起支付
     * @param dto
     * @return
     */
    @PostMapping("/apply")
    public JSONResult apply(@RequestBody @Valid CoursePayDto dto){
        String payStr = payOrderService.payApply(dto);
        return JSONResult.success(payStr);
    }
    /**
     * 支付宝的异步回调接口
     * @param dto
     * @return
     */
    @RequestMapping("/alipay/notify")
    public String notify(AlipayNotifyDto dto){
        try {
            String notify = payOrderService.notify(dto);
            return notify;
        }catch (Exception e){
            e.printStackTrace();
            return "Fail";
        }
    }
}
